﻿using System.Data;
using System.Linq;
using Domain.Abstract;
using Domain.Entity;

namespace Domain.Concrete
{
    public class EFFeedbackRepository : IFeedbackRepository
    {

        public EFDbContext Context { get; set; }

        public EFFeedbackRepository()
        {
            Context = EFDbContext.Instance;
        }

        public EFFeedbackRepository(EFDbContext context)
        {
            Context = context;
        }

        public IQueryable<Feedback> Feedbacks
        {
            get { return Context.Feedbacks; }
        }

        public void SaveFeedback(Feedback feedback)
        {
            if (feedback.Id == 0)
                Context.Feedbacks.Add(feedback);
            else
                Context.Entry(feedback).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void DeleteFeedback(Feedback feedback)
        {
            var dbFeedback = Feedbacks.SingleOrDefault(x => x.Id == feedback.Id);

            if(dbFeedback != null)
                Context.Feedbacks.Remove(dbFeedback);

            Context.SaveChanges();
        }

        public Feedback FindFeedback(int id)
        {
            return Feedbacks.FirstOrDefault(x => x.Id == id);   
        }
    }
}
